package com.beehoo.dcportal.admin.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.beehoo.dcportal.admin.dao.LeaderDao;
import com.beehoo.dcportal.admin.service.LeaderService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.WriteFile;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.common.base.BaseService;
import com.beehoo.dcportal.model.FileBean;
import com.beehoo.dcportal.model.LeaderBean;
import com.beehoo.dcportal.utils.CommonUtils;

public class LeaderServiceImpl extends BaseService implements LeaderService {

	private LeaderDao leaderDao;
	
	public void setLeaderDao(LeaderDao leaderDao) {
		this.leaderDao = leaderDao;
	}

	@Override
	public void addLeader(LeaderBean leaderBean) throws BaseException {
		validateLeaderBean(leaderBean);
		leaderBean = disposeUploadFile(leaderBean);
		String id = leaderDao.addLeader(leaderBean);
		if(CommonUtils.nvlToString(id).equals("")){
			throw new BaseException("custom.messages.error.leader.create.error");
		}
	}

	@Override
	public void delLeader(String leaderId) throws BaseException {
		if(CommonUtils.nvlToString(leaderId).equals("")){
			throw new BaseException("custom.messages.error.leader.id.empty");
		}
		int ret = leaderDao.delLeader(leaderId);
		if(ret <= 0){
			throw new BaseException("custom.messages.error.leader.del.error");
		}
	}

	@Override
	public LeaderBean queryLeader(String streetId, String leaderId)
			throws BaseException {
		if(CommonUtils.nvlToString(streetId).equals("")){
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if(CommonUtils.nvlToString(leaderId).equals("")){
			throw new BaseException("custom.messages.error.leader.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("leaderId", leaderId);
		return leaderDao.queryLeader(paramMap);
	}

	@Override
	public List<LeaderBean> queryLeaders(String streetId) throws BaseException {
		if(CommonUtils.nvlToString(streetId).equals("")){
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		return leaderDao.queryLeaders(paramMap);
	}

	@Override
	public void updateLeader(LeaderBean leaderBean) throws BaseException {
		validateLeaderBean(leaderBean);
		leaderBean = disposeUploadFile(leaderBean);
		int ret = leaderDao.updateLeader(leaderBean);
		if(ret <= 0){
			throw new BaseException("custom.messages.error.leader.update.error");
		}
	}
	
	private void validateLeaderBean(LeaderBean leaderBean) throws BaseException{
		if(leaderBean == null){
			throw new BaseException("custom.messages.error.leader.create.error");
		}
		if(CommonUtils.nvlToString(leaderBean.getStreetId()).equals("")){
			throw new BaseException("custom.messages.error.street.id.empty");
		}
		if(CommonUtils.nvlToString(leaderBean.getLeaderName()).equals("")){
			throw new BaseException("custom.messages.error.leader.name.empty");
		}
	}
	
	private LeaderBean disposeUploadFile(LeaderBean leaderBean) throws BaseException{
		FileBean fileBean = leaderBean.getFileBean();
		if(fileBean != null){
			int fileSize = fileBean.getFileSize();
			if(fileSize > Constant.UPLOAD_FILE_MAX_SIZE){
				throw new BaseException("struts.messages.error.file.too.large");
			}
			if(!CommonUtils.validateImageFileType(fileBean.getContentType())){
				throw new BaseException("struts.messages.error.content.type.not.allowed");
			}
			String fileName = CommonUtils.getUploadFileName(leaderBean.getStreetId(), Constant.IMG_LEADER_PHOTO);
			leaderBean.setLeaderPhoto(leaderBean.getPhotoDir() + fileName);
			
			File target = new File(leaderBean.getPhotoPath() + fileName);
			WriteFile wf = new WriteFile(fileBean.getFile(), target, fileSize);
			try {
				wf.write();
			} catch (BaseException e) {
				throw new BaseException("custom.messages.error.leader.upload.error");
			}
		}
		return leaderBean;
	}

}
